﻿package 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	
	public class SweepScroller extends MovieClip
	{
		//CONSTANTES.
		public static const ElasticEaseOut:uint = 0;
		public static const ElasticEaseIn:uint = 1;
		public static const RegularEaseOut:uint = 2;
		public static const RegularEaseIn:uint = 3;
		public static const BackEaseOut:uint = 4;
		public static const BackEaseIn:uint = 5;
		public static const BounceEaseOut:uint = 6;
		public static const BounceEaseIn:uint = 7;
		public static const StrongEaseOut:uint = 8;
		public static const StrongEaseIn:uint = 9;
		
		//VARIABLES INTERNAS SETEABLES OBLIGATORIAS.
			
			public var formClip:MovieClip;
			public var targetClip:MovieClip;
		
		//VARIABLES INTERNAS SETEABLES OPCIONALES.
		
			public var vAlign:String = "right";
			public var hAlign:String = "bottom";
			public var useHand:Boolean = true;
			public var motionType:uint = RegularEaseOut;
			public var motionTime:Number = 0.5;
			public var disabledBtnAlpha:Number = 0.25;
		
		//VARIABLES INTERNAS.
		private var box:MovieClip;
		private var contenido:MovieClip;
		private var length:Number;
		private var direction:String;
		private var prevBtn:MovieClip;
		private var nextBtn:MovieClip;
		private var currentPage:uint = 0;
		private var motionTween:Tween;
		private var pages:uint;
		private var easeArray:Array = [
									   Elastic.easeOut,
									   Elastic.easeIn,
									   Regular.easeOut,
									   Regular.easeIn,
									   Back.easeOut,
									   Back.easeIn,
									   Bounce.easeOut,
									   Bounce.easeIn,
									   Strong.easeOut,
									   Strong.easeIn
									  ];
		
		//CONSTRUCTOR.
		public function SweepScroller()
		{
			
		}
		
		//INIT.
		public function init():void
		{
			//IDENTIFICA ELEMENTOS DEL TARGETCLIP.
			box = targetClip.box;
			contenido = MovieClip(targetClip.getChildByName("contentHolder"));
			
			//PRECALCULA VALORES DE SCROLLING.
			direction = contenido.width > box.width ? "horizontal" : "vertical";
			length = direction == "horizontal" ? box.width : box.height;
			pages = direction == "horizontal" ? Math.floor(contenido.width/length) : Math.floor(contenido.height/length);
			
			//INICIALIZA LOS BOTONES.
			initBtns();
		}
		
		//INICIALIZA LOS BOTONES.
		private function initBtns():void
		{	
			//LOS IDENTIFICA.
			prevBtn = formClip.prevBtn;
			nextBtn = formClip.nextBtn;
			
			//LOS TRANSFIERE AL DISPLAYER.
			targetClip.addChild(prevBtn);
			targetClip.addChild(nextBtn);
			
			//ACTIVA LAS MANITOS.
			prevBtn.buttonMode = useHand;
			prevBtn.useHandCursor = useHand;
			nextBtn.buttonMode = useHand;
			nextBtn.useHandCursor = useHand;
			
			//ASOCIA LA FUNCION DE CADA BOTON (evalBtns() ADMINISTRA LOS LISTENERS).
			prevBtn.relatedFunc = prevPage;
			nextBtn.relatedFunc = nextPage;
			
			//DETERMINA LA HABILITACION DE LOS BOTONES.
			evalBtns();
			
			//LOS POSICIONA.
			placeBtns();
		}
		
		//POSICIONA LOS BOTONES.
		private function placeBtns():void
		{
			if (direction == "vertical")
			{
				prevBtn.rotation = -180;
				if (vAlign == "right")
				{
					prevBtn.x = box.x + box.width + prevBtn.width/2;
					prevBtn.y = prevBtn.height/2;
					nextBtn.y = box.y + box.height - prevBtn.height/2;
				}
				else if (vAlign == "left")
				{
					prevBtn.x = -prevBtn.width/2;
					prevBtn.y = prevBtn.height/2;
					nextBtn.y = box.y + box.height - prevBtn.height/2;
				}
				nextBtn.x = prevBtn.x;
			}
			else if (direction == "horizontal")
			{
				prevBtn.rotation = 90;
				nextBtn.rotation = -90;
				if (hAlign == "top")
				{
					prevBtn.x = prevBtn.width/2;
					prevBtn.y = -prevBtn.height/2;
					nextBtn.x = box.x + box.width - prevBtn.width/2;
				}
				else if (hAlign == "bottom")
				{
					prevBtn.x = prevBtn.width/2;
					prevBtn.y = box.y + box.height + prevBtn.height/2;
					nextBtn.x = box.x + box.width - prevBtn.width/2;
				}
				nextBtn.y = prevBtn.y;
			}
		}
		
		//PROXIMA.
		private function nextPage(evt:MouseEvent):void
		{
			currentPage = currentPage + 1 < pages ? currentPage + 1 : pages;
			moveContent();
		}
		
		//ANTERIOR.
		private function prevPage(evt:MouseEvent):void
		{
			currentPage = currentPage - 1 > 0 ? currentPage - 1 : 0;
			moveContent();
		}
		
		//MUEVE EL CONTENIDO.
		private function moveContent():void
		{
			//DETERMINA LOS PARAMETROS DEL MOVIMIENTO.
			var prop:String = direction == "horizontal" ? "x" : "y";
			var curr:Number = direction == "horizontal" ? contenido.x : contenido.y;
			var targetPos:Number = direction == "horizontal" ? -targetClip.itemW*targetClip.scrollPageW*currentPage : -targetClip.itemH*targetClip.scrollPageH*currentPage;
			
			//DETERMINA LA HABILITACION DE LOS BOTONES.
			evalBtns();
			
			//CORTA CUALQUIER TWEEN ANTERIOR.
			if(motionTween != null)
				motionTween.stop();
			
			//DISPARA EL MOVIMIENTO.
			motionTween = new Tween(contenido, prop, easeArray[motionType], curr, targetPos, motionTime, true);
		}
		
		//EVALUA QUE BOTONES ESTAN HABILITADOS.
		private function evalBtns():void
		{
			if (currentPage < pages) habilitarBtn(nextBtn);
			else deshabilitarBtn(nextBtn);
			if (currentPage >= 1) habilitarBtn(prevBtn);
			else deshabilitarBtn(prevBtn);
		}
		
		//HABILITA/DESHABLITA UN BOTON.
		private function habilitarBtn(btn:MovieClip):void
		{
			btn.alpha = 100;
			btn.hit.addEventListener(MouseEvent.CLICK, btn.relatedFunc);
			btn.buttonMode = useHand;
			btn.useHandCursor = useHand;
		}
		private function deshabilitarBtn(btn:MovieClip):void
		{
			btn.alpha = disabledBtnAlpha;
			btn.hit.removeEventListener(MouseEvent.CLICK, btn.relatedFunc);
			btn.buttonMode = false;
			btn.useHandCursor = false;
		}
		
		//INFO.
		public function info():void
		{
			var str:String = "SweepScroller v1.0" + String.fromCharCode(13);
			str += "---------------" + String.fromCharCode(13);
			str += "VARIABLES SETEABLES OBLIGATORIAS:" + String.fromCharCode(13);
			str += "formClip:MovieClip" + String.fromCharCode(13);
			str += "targetClip:MovieClip" + String.fromCharCode(13);
			str += "---------------" + String.fromCharCode(13);
			str += "VARIABLES SETEABLES OPCIONALES:" + String.fromCharCode(13);
			str += "vAlign:String [default = right]" + String.fromCharCode(13);
			str += "hAlign:String [default = bottom]" + String.fromCharCode(13);
			str += "useHand:Boolean [default = true]" + String.fromCharCode(13);
			str += "motionType:uint [default = RegularEaseOut]" + String.fromCharCode(13);
			str += "motionTime:Number [default = 0.5]" + String.fromCharCode(13);
			str += "disabledBtnAlpha:Number [default =0.25]" + String.fromCharCode(13);
			str += "---------------" + String.fromCharCode(13);
			str += "NOTAS:" + String.fromCharCode(13);
			str += "Requiere un clip de forma." + String.fromCharCode(13);
			str += "Requiere que se le asocie un clip a scrollear." + String.fromCharCode(13);
			str += "El clip a scrollear tiene que ser un ContentDisplayer, o sea, tener un contentHolder y un box como minimo." + String.fromCharCode(13);
			str += "---------------";
			trace(str);
		}
	}
	
}